package com.yc.book.servlet;

import com.yc.book.biz.BookBiz;
import com.yc.book.util.DBUtil;
import com.yc.book.util.Utils;
import com.yc.book.vo.Result;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet({"/book/*"})
public class BookServlet extends BaseServlet{

    public Object query(HttpServletRequest request, HttpServletResponse response){
        DBUtil db = new DBUtil();
        String sql = "select * from books where 1=1";
        List<Object> params = new ArrayList<Object>();
        String bookname = request.getParameter("bookname");
        String bookauthor = request.getParameter("bookauthor");
        String bookpress = request.getParameter("bookpress");
        String pressdate = request.getParameter("pressdate");
      /*  if(bookname != null && !bookname.trim().isEmpty()){
            sql += " and bookname like ?";
            params.add("%" + bookname + "%");
        }
        if(bookauthor != null && !bookauthor.trim().isEmpty()){
            sql += " and bookauthor like ?";
            params.add("%" + bookauthor + "%");
        }
        if(bookpress != null && !bookpress.trim().isEmpty()){
            sql += " and bookpress like ?";
            params.add("%" + bookpress + "%");
        }
        if(pressdate != null && !pressdate.trim().isEmpty()){
            sql += " and pressdate like ?";
            //尝试让mysql自动转换日期格式
            params.add(pressdate.substring(0, 10));
        }*/
        sql += Utils.appendSqlParameter(bookname," and bookname like concat('%',?,'%')",params);
        sql += Utils.appendSqlParameter(bookauthor," and bookauthor like concat('%',?,'%')",params);
        sql += Utils.appendSqlParameter(bookpress," and bookpress like concat('%',?,'%')",params);
        sql += Utils.appendSqlParameter(pressdate," and pressdate = substr(?,1,10)",params);

        //查询总记录数
        int total = db.total("select count(*) from books");

        //获取分页参数
        String page = request.getParameter("page");//当前页
        String rows = request.getParameter("rows");//每页几条记录
        //mysql 分页语法 select ... limit 起始位置,长度
        int ipage = Integer.parseInt(page);
        int irows = Integer.parseInt(rows);
        int begin = (ipage - 1) * irows;
        sql += " limit ?,?";
        params.add(begin);
        params.add(irows);

        //当前页的数据
        List<?> data = db.listObject(sql,params.toArray());

        //封装返回结果
        return Map.of("total",total,"data",data);
    }

    public List<Map<String,Object>> findById(HttpServletRequest request, HttpServletResponse response){
        DBUtil db = new DBUtil();
        return db.listObject("select * from books where id=?",request.getParameter("id"));
    }

    public Map<String,Object> find(HttpServletRequest request, HttpServletResponse response){
        DBUtil db = new DBUtil();
        return db.findObject("select * from books where id=?",request.getParameter("id"));
    }

    public List<Map<String,Object>> queryPressItems(HttpServletRequest request, HttpServletResponse response){
        DBUtil db = new DBUtil();
        return db.listObject("select bookpress from books ");
    }

    private BookBiz biz=new BookBiz();

    public Result save(HttpServletRequest request ,HttpServletResponse response){
        String id = request.getParameter("id");
        String bookname = request.getParameter("bookname");
        String bookpress = request.getParameter("bookpress");
        String pressdate = request.getParameter("pressdate");
        String bookauthor = request.getParameter("bookauthor");
        String bookcount = request.getParameter("bookcount");
        String bookimage = request.getParameter("bookimage");
        biz.save(id,bookname,bookpress,pressdate,bookauthor,bookcount,bookimage);
        return new Result(1,"添加成功",null);
    }




    public  Result delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        biz.delete(id);
        return new Result(1,"删除成功",null);
    }


}
